package com.gorkr.offer;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/07/27 09:53
 **/
public class M12JuZhenZhongDeLuJingIcof {


    public boolean exist(char[][] board, String word) {
        int m = board.length;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(board[i][j]==word.charAt(0)){
                    boolean[][] visited = new boolean[m][n];
                    if(traverse(i,j,board,visited,1,word,m,n,false)){
                        return true;
                    }
                }
            }
        }
        return false;

    }
    //visited 处理

    private boolean traverse(int i, int j, char[][] board, boolean[][] visited, int index, String word, int m, int n, boolean res) {
        if (index == word.length()) {

            return true;
        }

        visited[i][j] = true;

        //左边
        if (j - 1 >= 0 && !visited[i][j - 1] && board[i][j - 1] == word.charAt(index)){
            res = traverse(i, j - 1, board, visited, index + 1, word, m, n,res);
            visited[i][j - 1] = false;
        }
        // 右
        if (j + 1 <= n - 1 && !visited[i][j + 1] && board[i][j + 1] == word.charAt(index)) {
            res = traverse(i, j + 1, board, visited, index + 1, word, m, n,res);
            visited[i][j + 1] = false;
        }
        // 上
        if (i - 1 >= 0 && !visited[i - 1][j] && board[i - 1][j] == word.charAt(index)) {
            res = traverse(i - 1, j, board, visited, index + 1, word, m, n,res);
            visited[i - 1][j] = false;
        }
        // 下
        if (i + 1 <= m - 1 && !visited[i + 1][j] && board[i + 1][j] == word.charAt(index)) {
            res =traverse(i + 1, j, board, visited, index + 1, word, m, n,res);
            visited[i + 1][j] = false;
        }
        return res;
    }

    public boolean exist2(char[][] board, String word) {
        char[] words = word.toCharArray();
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                if(dfs(board, words, i, j, 0)) {
                    return true;
                }
            }
        }
        return false;
    }
    boolean dfs(char[][] board, char[] word, int i, int j, int k) {
        if(i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) {
            return false;
        }
        if(k == word.length - 1) {
            return true;
        }
        board[i][j] = '\0';
        // 下 上 右 左
        boolean res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) ||
                dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i , j - 1, k + 1);
        board[i][j] = word[k];
        return res;
    }


    @Test
    public void test(){
        char[][] board = {{'A','B','C','E'},{'S','F','C','S'}, {'A','D','E','E'}};
        String word = "AE";
        System.out.println(exist2(board, word));
    }
}
    